package com.itboye.ihomebank.activity.finance.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.P2PQueryBean;
import com.itboye.ihomebank.activity.finance.InvestmentListActivity;
import com.itboye.ihomebank.util.DoubleUtil;
import com.itboye.ihomebank.util.StringFormatUtil;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 投资
 */
public class TouZiAdapterTwo extends BaseAdapter {
    Context context;
    List<P2PQueryBean.A> list;

    public TouZiAdapterTwo(Context context,List<P2PQueryBean.A> list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if(view==null){
            holder=new ViewHolder();
            view= LayoutInflater.from(context).inflate(R.layout.item_touzione, null);
            holder.touzi_title=(TextView) view.findViewById(R.id.touzi_title);
            holder.touzi_biaoqian=(TextView) view.findViewById(R.id.touzi_biaoqian);
            holder.touzi_baifen=(TextView) view.findViewById(R.id.touzi_baifen);
            holder.touzi_yue=(TextView) view.findViewById(R.id.touzi_yue);
            holder.touzi_shengyu=(TextView) view.findViewById(R.id.touzi_shengyu);
            holder.touzi_bai=(TextView) view.findViewById(R.id.touzi_bai);
            holder.touzi_jiahua=(TextView) view.findViewById(R.id.touzi_jiahua);
            holder.touzi_name=(TextView) view.findViewById(R.id.touzi_name);
            holder.seekbar= (ProgressBar) view.findViewById(R.id.seekbar);

            view.setTag(holder);
        }else{
            holder=(ViewHolder) view.getTag();
        }
        holder.touzi_title.setText(list.get(position).getTitle());
        String wholeStr = "剩余"+ DoubleUtil.doubleTransform(list.get(position).getRemaining_amount())+"元";
        StringFormatUtil spanStr = new StringFormatUtil(context, wholeStr,
                DoubleUtil.doubleTransform(list.get(position).getRemaining_amount()), R.color.orange_yelow).fillColor();
        holder.touzi_shengyu.setText(spanStr.getResult());
        holder.touzi_biaoqian.setVisibility(Integer.parseInt(list.get(position).getDt_tag())<=0?View.GONE:View.VISIBLE);
        holder.touzi_biaoqian.setText(list.get(position).getDt_tag_name());

        if (Integer.parseInt(list.get(position).getProject_duration_month())<=0) {
            if (Integer.parseInt(list.get(position).getProject_duration_day())<=0) {

            }else{
                holder.touzi_yue.setText(list.get(position).getProject_duration_day()+"天");
            }
        }else{
            holder.touzi_yue.setText(list.get(position).getProject_duration_month()+"个月");
        }
        holder.touzi_name.setText(list.get(position).getDt_repayment_type_name());
        holder.touzi_baifen.setText(DoubleUtil.doubleTransform(list.get(position).getExpected_annualize())+"%");

        //进度条
        Double jindu = Double.parseDouble(list.get(position).getTotal_money());
        Double jin = Double.parseDouble(list.get(position).getRemaining_amount());
        Double jingtutiao = jindu - jin;
        Double douNumber = jingtutiao / jindu;
        DecimalFormat df = new DecimalFormat("0");
        Double x=douNumber*1000;
        int i=Integer.parseInt(df.format(x));
        holder.touzi_bai.setText(df.format(douNumber * 100) + "%");
        holder.seekbar.setMax(1000);
        holder.seekbar.setProgress(i);
        holder.touzi_jiahua.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, InvestmentListActivity.class);
                intent.putExtra("rate_year", list.get(position).getExpected_annualize());
                intent.putExtra("money", list.get(position).getTotal_money());
                intent.putExtra("month", list.get(position).getProject_duration_month());
                intent.putExtra("day", list.get(position).getProject_duration_day());
                intent.putExtra("pay_type", list.get(position).getDt_repayment_type());
                if (isDateStringValid(list.get(position).getProject_duration_month())) {
                    if (list.get(position).getProject_duration_month().length() == 10) {
                        intent.putExtra("start_time", list.get(position).getProject_duration_month());
                    } else {
                        Log.d("不是日期", "不是日期格式"+onSimleData());
                        intent.putExtra("start_time", onSimleData());
                    }
                } else {
                    intent.putExtra("start_time", onSimleData());
                    Log.d("不是日期", "不是日期格式"+onSimleData());
                }
                context.startActivity(intent);
            }
        });

        return view;

    }
    /**
     * 判断字符串是否为日期格式
     *
     * @param date
     * @return
     */
    public boolean isDateStringValid(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 输入对象不为空
        try {
            sdf.parse(date);
            return true;
        } catch (java.text.ParseException e) {
            return false;
        }
    }

    /**
     * 得到系统时间
     * @return
     */
    public String onSimleData() {
        String str;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        str = formatter.format(curDate);
        return str;
    }
    class ViewHolder{
        TextView touzi_title,touzi_biaoqian,touzi_baifen,touzi_yue,touzi_shengyu,touzi_bai,touzi_jiahua,touzi_name;
        public ProgressBar seekbar;
    }
}
